package cn.iocoder.yudao.module.vote.controller.admin.voteHead;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vote.controller.admin.voteHead.vo.*;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteHead.VoteHeadDO;
import cn.iocoder.yudao.module.vote.service.voteHead.VoteHeadService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 投票头")
@RestController
@RequestMapping("/vote/head")
@Validated
public class VoteHeadController {

    @Resource
    private VoteHeadService headService;

    @PostMapping("/create")
    @Operation(summary = "创建投票头")
    @PreAuthorize("@ss.hasPermission('vote:head:create')")
    public CommonResult<Long> createHead(@Valid @RequestBody VoteHeadSaveReqVO createReqVO) {
        return success(headService.createHead(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新投票头")
    @PreAuthorize("@ss.hasPermission('vote:head:update')")
    public CommonResult<Boolean> updateHead(@Valid @RequestBody VoteHeadSaveReqVO updateReqVO) {
        headService.updateHead(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除投票头")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vote:head:delete')")
    public CommonResult<Boolean> deleteHead(@RequestParam("id") Long id) {
        headService.deleteHead(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得投票头")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vote:head:query')")
    public CommonResult<VoteHeadRespVO> getHead(@RequestParam("id") Long id) {
        VoteHeadDO head = headService.getHead(id);
        return success(BeanUtils.toBean(head, VoteHeadRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得投票头分页")
    @PreAuthorize("@ss.hasPermission('vote:head:query')")
    public CommonResult<PageResult<VoteHeadRespVO>> getHeadPage(@Valid VoteHeadPageReqVO pageReqVO) {
        PageResult<VoteHeadDO> pageResult = headService.getHeadPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VoteHeadRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出投票头 Excel")
    @PreAuthorize("@ss.hasPermission('vote:head:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportHeadExcel(@Valid VoteHeadPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VoteHeadDO> list = headService.getHeadPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "投票头.xls", "数据", VoteHeadRespVO.class,
                        BeanUtils.toBean(list, VoteHeadRespVO.class));
    }

}